package com.pxmeta.admin.controller;

import cn.authing.core.auth.AuthenticationClient;
import cn.authing.core.graphql.GraphQLException;
import cn.authing.core.mgmt.UsersManagementClient;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.pxmeta.circle.service.ICircleService;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserIdentify;
import com.pxmeta.user.entity.dto.VerifyCertDTO;
import com.pxmeta.user.entity.vo.SubordinateUserVO;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.user.service.IUserIdentifyService;
import com.pxmeta.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "下级管理",tags = "下级用户")
@RequestMapping(value = "/subordinate")
@CrossOrigin
@RequiredArgsConstructor
public class SubordinateController {

    private final IUserIdentifyService userIdentifyService;
    private final IUserService userService;
    private final ICircleService circleService;

    @GetMapping("/getSubordinate")
    public AjaxResult<List<SubordinateUserVO>> getSubordinate(@RequestParam Integer page,
                                                              @RequestParam Integer size,
                                                              @RequestParam(required = false) String phone) {
        if (StringUtils.isBlank(phone)){
            return AjaxResult.success();
        }
        IPage<SubordinateUserVO> result = userService.getSubordinateUsers(phone,page, size);

        return AjaxResult.successPage(result.getRecords(),result.getTotal());
    }

    @GetMapping("/getTotalCommission")
    @ApiOperation("获取分享总收益")
    public AjaxResult getTotalCommission(@RequestParam String phone) {
        Double commission = userService.getTotalCommission(phone);

        return AjaxResult.success(commission);
    }


    @GetMapping("/getShareWeekProfit")
    @ApiOperation("获取分享周收益")
    public AjaxResult getShareWeekProfit(){
        Long userId = SecurityUtils.getUserId();
        String phone = userService.getById(userId).getPhone();
        Double commission = userService.getTotalCommission(phone);
        return AjaxResult.success(commission);
    }


    @GetMapping("/getShareUsers")
    @ApiOperation("获取分享用户列表")
    public AjaxResult<List<SubordinateUserVO>> getShareUsers(@RequestParam Integer page,
                                                             @RequestParam Integer size){
        Long userId = SecurityUtils.getUserId();
        String phone = userService.getById(userId).getPhone();
        IPage<SubordinateUserVO> result = userService.getSubordinateUsers(phone, page, size);
        return AjaxResult.success(result.getRecords());
    }



    @PostMapping("/verify")
    public AjaxResult<Void> verify(@RequestBody VerifyCertDTO dto){
        Long id = dto.getId();
        UserIdentify userIdentify = userIdentifyService.getById(id);
        Integer status = dto.getStatus();
        userIdentify.setStatus(status);
        userIdentify.setReason(dto.getReason());
        userIdentifyService.updateById(userIdentify);
        User user = userService.getById(userIdentify.getUserId());
        if (status == 1){
            user.setSchoolName(userIdentify.getOrgName());
            user.setIsStudent(1);
            userService.updateById(user);
            circleService.handleVerifyStudentSuccess(user.getId());
        }else {
            user.setSchoolName("");
            user.setIsStudent(0);
            userService.updateById(user);
        }
        return AjaxResult.success();
    }




}
